FROM rust:alpine as backend
WORKDIR /home/rust/src
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk --no-cache add musl-dev openssl-dev protoc
RUN export RUSTUP_DIST_SERVER="https://rsproxy.cn"
RUN export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
RUN rustup component add rustfmt
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/home/rust/src/target \
    cargo build --release --bin sshx-server && \
    cp target/release/sshx-server /usr/local/bin

FROM node:lts-alpine as frontend
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk --no-cache add git
WORKDIR /usr/src/app
COPY . .
RUN npm config set registry https://registry.npmmirror.com && \
    npm ci
RUN npm run build

# FROM alpine:latest
# #设置时区
# ENV TZ=Asia/Shanghai
# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
#     apk update --no-cache && \
#     apk upgrade && \
#     ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && \
#     echo $TZ > /etc/timezone && \
#     rm -rf /var/cache/apk/* /tmp/* /var/tmp/* /var/lib/apt/lists/* \
#     /usr/share/doc/* /name/usr/share/man/* /usr/share/info/*
# WORKDIR /root
# COPY --from=frontend /usr/src/app/build build
# COPY --from=backend /usr/local/bin/sshx-server .
# CMD ["./sshx-server", "--listen", "::"]
# EXPOSE 8051

FROM scratch
COPY --from=frontend /usr/src/app/build build
COPY --from=backend /usr/local/bin/sshx-server .
CMD ["./sshx-server", "--listen", "::"]
EXPOSE 8051